{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from pandas import Series, DataFrame\n",
    "\n",
    "np.random.seed(12345)\n",
    "plt.rc(\"figure\", figsize=(10, 6))\n",
    "PREVIOUS_MAX_ROWS = pd.options.display.max_rows\n",
    "pd.options.display.max_rows = 20\n",
    "pd.options.display.max_columns = 20\n",
    "pd.options.display.max_colwidth = 80\n",
    "np.set_printoptions(precision=4, suppress=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:15.149393Z",
     "end_time": "2024-04-17T17:37:19.841127Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.1 pandas的数据结构介绍"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Series"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "0    4\n1    7\n2   -5\n3    3\ndtype: int64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([4, 7, -5, 3])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.141354Z",
     "end_time": "2024-04-17T17:37:19.864143Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 4,  7, -5,  3], dtype=int64)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.151062Z",
     "end_time": "2024-04-17T17:37:19.864143Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "RangeIndex(start=0, stop=4, step=1)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.163722Z",
     "end_time": "2024-04-17T17:37:19.864143Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "d    4\nb    7\na   -5\nc    3\ndtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2 = Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])\n",
    "obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.168928Z",
     "end_time": "2024-04-17T17:37:19.864143Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['d', 'b', 'a', 'c'], dtype='object')"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.179938Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "-5"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2['a']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.193899Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "c    3\na   -5\nd    6\ndtype: int64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2['d'] = 6\n",
    "obj2[['c', 'a', 'd']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.200456Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "d    6\nb    7\nc    3\ndtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2[obj2 > 0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.210021Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "d    12\nb    14\na   -10\nc     6\ndtype: int64"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2 * 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.216777Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "d     403.428793\nb    1096.633158\na       0.006738\nc      20.085537\ndtype: float64"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.224151Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'b' in obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.231615Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'e' in obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "Ohio      35000\nTexas     71000\nOregon    16000\nUtah       5000\ndtype: int64"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata = {\"Ohio\": 35000, \"Texas\": 71000, \"Oregon\": 16000, \"Utah\": 5000}\n",
    "obj3 = Series(sdata)\n",
    "obj3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "{'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj3.to_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "California        NaN\nOhio          35000.0\nOregon        16000.0\nTexas         71000.0\ndtype: float64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states = [\"California\", \"Ohio\", \"Oregon\", \"Texas\"]\n",
    "obj4 = Series(sdata, index=states)\n",
    "obj4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "California     True\nOhio          False\nOregon        False\nTexas         False\ndtype: bool"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.isnull(obj4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "California    False\nOhio           True\nOregon         True\nTexas          True\ndtype: bool"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.notnull(obj4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.865127Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "California     True\nOhio          False\nOregon        False\nTexas         False\ndtype: bool"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj4.isnull()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "Ohio      35000\nTexas     71000\nOregon    16000\nUtah       5000\ndtype: int64"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "California        NaN\nOhio          35000.0\nOregon        16000.0\nTexas         71000.0\ndtype: float64"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "California         NaN\nOhio           70000.0\nOregon         32000.0\nTexas         142000.0\nUtah               NaN\ndtype: float64"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj3 + obj4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.310289Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "state\nCalifornia        NaN\nOhio          35000.0\nOregon        16000.0\nTexas         71000.0\nName: population, dtype: float64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj4.name = 'population'\n",
    "obj4.index.name = 'state'\n",
    "obj4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.314295Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0    4\n1    7\n2   -5\n3    3\ndtype: int64"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.325206Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "Bob      4\nSteve    7\nJeff    -5\nRyan     3\ndtype: int64"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.376570Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### DataFrame"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "    state  year  pop\n0    Ohio  2000  1.5\n1    Ohio  2001  1.7\n2    Ohio  2002  3.6\n3  Nevada  2001  2.4\n4  Nevada  2002  2.9\n5  Nevada  2003  3.2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>state</th>\n      <th>year</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>1.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Ohio</td>\n      <td>2001</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Ohio</td>\n      <td>2002</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nevada</td>\n      <td>2001</td>\n      <td>2.4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Nevada</td>\n      <td>2002</td>\n      <td>2.9</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Nevada</td>\n      <td>2003</td>\n      <td>3.2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"state\": [\"Ohio\", \"Ohio\", \"Ohio\", \"Nevada\", \"Nevada\", \"Nevada\"],\n",
    "        \"year\": [2000, 2001, 2002, 2001, 2002, 2003],\n",
    "        \"pop\": [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}\n",
    "frame = DataFrame(data)\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.376570Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "    state  year  pop\n0    Ohio  2000  1.5\n1    Ohio  2001  1.7\n2    Ohio  2002  3.6\n3  Nevada  2001  2.4\n4  Nevada  2002  2.9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>state</th>\n      <th>year</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>1.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Ohio</td>\n      <td>2001</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Ohio</td>\n      <td>2002</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nevada</td>\n      <td>2001</td>\n      <td>2.4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Nevada</td>\n      <td>2002</td>\n      <td>2.9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.422032Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "   year   state  pop\n0  2000    Ohio  1.5\n1  2001    Ohio  1.7\n2  2002    Ohio  3.6\n3  2001  Nevada  2.4\n4  2002  Nevada  2.9\n5  2003  Nevada  3.2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataFrame(data, columns=['year', 'state', 'pop'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.430777Z",
     "end_time": "2024-04-17T17:37:19.866140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "       year   state  pop debt\none    2000    Ohio  1.5  NaN\ntwo    2001    Ohio  1.7  NaN\nthree  2002    Ohio  3.6  NaN\nfour   2001  Nevada  2.4  NaN\nfive   2002  Nevada  2.9  NaN\nsix    2003  Nevada  3.2  NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n      <th>debt</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>four</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>five</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>six</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2 = DataFrame(data, columns=[\"year\", \"state\", \"pop\", \"debt\"], index=['one', 'two', 'three', 'four', 'five', 'six'])\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.452764Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['year', 'state', 'pop', 'debt'], dtype='object')"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.472388Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "one        Ohio\ntwo        Ohio\nthree      Ohio\nfour     Nevada\nfive     Nevada\nsix      Nevada\nName: state, dtype: object"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2['state']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.478383Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "one      2000\ntwo      2001\nthree    2002\nfour     2001\nfive     2002\nsix      2003\nName: year, dtype: int64"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.year"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.488379Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "year     2002\nstate    Ohio\npop       3.6\ndebt      NaN\nName: three, dtype: object"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.loc['three']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.498828Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "year     2002\nstate    Ohio\npop       3.6\ndebt      NaN\nName: three, dtype: object"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.iloc[2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.505532Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "       year   state  pop  debt\none    2000    Ohio  1.5  16.5\ntwo    2001    Ohio  1.7  16.5\nthree  2002    Ohio  3.6  16.5\nfour   2001  Nevada  2.4  16.5\nfive   2002  Nevada  2.9  16.5\nsix    2003  Nevada  3.2  16.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n      <th>debt</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>four</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>five</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>six</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n      <td>16.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2['debt'] = 16.5\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.515138Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "       year   state  pop  debt\none    2000    Ohio  1.5   0.0\ntwo    2001    Ohio  1.7   1.0\nthree  2002    Ohio  3.6   2.0\nfour   2001  Nevada  2.4   3.0\nfive   2002  Nevada  2.9   4.0\nsix    2003  Nevada  3.2   5.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n      <th>debt</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>four</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>five</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>six</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n      <td>5.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2['debt'] = np.arange(6.)\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.522918Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "two    -1.2\nfour   -1.5\nfive   -1.7\ndtype: float64"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val = Series([-1.2, -1.5, -1.7], index=[\"two\", \"four\", \"five\"])\n",
    "val"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.545033Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "       year   state  pop  debt\none    2000    Ohio  1.5   NaN\ntwo    2001    Ohio  1.7  -1.2\nthree  2002    Ohio  3.6   NaN\nfour   2001  Nevada  2.4  -1.5\nfive   2002  Nevada  2.9  -1.7\nsix    2003  Nevada  3.2   NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n      <th>debt</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n      <td>-1.2</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>four</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n      <td>-1.5</td>\n    </tr>\n    <tr>\n      <th>five</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n      <td>-1.7</td>\n    </tr>\n    <tr>\n      <th>six</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2[\"debt\"] = val\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.565997Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "       year   state  pop  debt  eastern\none    2000    Ohio  1.5   NaN     True\ntwo    2001    Ohio  1.7  -1.2     True\nthree  2002    Ohio  3.6   NaN     True\nfour   2001  Nevada  2.4  -1.5    False\nfive   2002  Nevada  2.9  -1.7    False\nsix    2003  Nevada  3.2   NaN    False",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>state</th>\n      <th>pop</th>\n      <th>debt</th>\n      <th>eastern</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>2000</td>\n      <td>Ohio</td>\n      <td>1.5</td>\n      <td>NaN</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2001</td>\n      <td>Ohio</td>\n      <td>1.7</td>\n      <td>-1.2</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2002</td>\n      <td>Ohio</td>\n      <td>3.6</td>\n      <td>NaN</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>four</th>\n      <td>2001</td>\n      <td>Nevada</td>\n      <td>2.4</td>\n      <td>-1.5</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>five</th>\n      <td>2002</td>\n      <td>Nevada</td>\n      <td>2.9</td>\n      <td>-1.7</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>six</th>\n      <td>2003</td>\n      <td>Nevada</td>\n      <td>3.2</td>\n      <td>NaN</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2['eastern'] = frame2.state == 'Ohio'\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.586993Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['year', 'state', 'pop', 'debt'], dtype='object')"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del frame2['eastern']\n",
    "frame2.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.595710Z",
     "end_time": "2024-04-17T17:37:19.867126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "      Nevada  Ohio\n2001     2.4   1.7\n2002     2.9   3.6\n2000     NaN   1.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Nevada</th>\n      <th>Ohio</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2001</th>\n      <td>2.4</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>2.9</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>2000</th>\n      <td>NaN</td>\n      <td>1.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pop = {'Nevada': {2001: 2.4, 2002: 2.9}, 'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}\n",
    "frame3 = DataFrame(pop)\n",
    "frame3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.608906Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "        2001  2002  2000\nNevada   2.4   2.9   NaN\nOhio     1.7   3.6   1.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>2001</th>\n      <th>2002</th>\n      <th>2000</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Nevada</th>\n      <td>2.4</td>\n      <td>2.9</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>1.7</td>\n      <td>3.6</td>\n      <td>1.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame3.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.616672Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "      Nevada  Ohio\n2001     2.4   1.7\n2002     2.9   3.6\n2003     NaN   NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Nevada</th>\n      <th>Ohio</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2001</th>\n      <td>2.4</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>2.9</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>2003</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataFrame(pop, index=[2001, 2002, 2003])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.625970Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "      Ohio  Nevada\n2001   1.7     2.4\n2002   3.6     2.9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2001</th>\n      <td>1.7</td>\n      <td>2.4</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>3.6</td>\n      <td>2.9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdata = {'Ohio': frame3['Ohio'][:-1], 'Nevada': frame3['Nevada'][:2]}\n",
    "DataFrame(pdata)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.637397Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "state  Nevada  Ohio\nyear               \n2001      2.4   1.7\n2002      2.9   3.6\n2000      NaN   1.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>state</th>\n      <th>Nevada</th>\n      <th>Ohio</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2001</th>\n      <td>2.4</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>2.9</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>2000</th>\n      <td>NaN</td>\n      <td>1.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame3.index.name = \"year\"\n",
    "frame3.columns.name = \"state\"\n",
    "frame3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.646302Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[2.4, 1.7],\n       [2.9, 3.6],\n       [nan, 1.5]])"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame3.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.652141Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[2000, 'Ohio', 1.5, nan],\n       [2001, 'Ohio', 1.7, -1.2],\n       [2002, 'Ohio', 3.6, nan],\n       [2001, 'Nevada', 2.4, -1.5],\n       [2002, 'Nevada', 2.9, -1.7],\n       [2003, 'Nevada', 3.2, nan]], dtype=object)"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.658685Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 索引对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['a', 'b', 'c'], dtype='object')"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(range(3), index=['a', 'b', 'c'])\n",
    "index = obj.index\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.665699Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['b', 'c'], dtype='object')"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index[1:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.672561Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "Index([0, 1, 2], dtype='int32')"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels = pd.Index(np.arange(3))\n",
    "labels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.679079Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1.5\n1   -2.5\n2    0.0\ndtype: float64"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2 = Series([1.5, -2.5, 0], index=labels)\n",
    "obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.685821Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2.index is labels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.692526Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "data": {
      "text/plain": "state  Nevada  Ohio\nyear               \n2001      2.4   1.7\n2002      2.9   3.6\n2000      NaN   1.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>state</th>\n      <th>Nevada</th>\n      <th>Ohio</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2001</th>\n      <td>2.4</td>\n      <td>1.7</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>2.9</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>2000</th>\n      <td>NaN</td>\n      <td>1.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.698557Z",
     "end_time": "2024-04-17T17:37:19.868126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['Nevada', 'Ohio'], dtype='object', name='state')"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame3.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.706831Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Ohio' in frame3.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.712383Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2003 in frame3.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.718665Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['foo', 'foo', 'bar', 'bar'], dtype='object')"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dup_labels = pd.Index(['foo', 'foo', 'bar', 'bar'])\n",
    "dup_labels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.724783Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.2 基本功能"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 重新索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "d    4.5\nb    7.2\na   -5.3\nc    3.6\ndtype: float64"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.733075Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "a   -5.3\nb    7.2\nc    3.6\nd    4.5\ne    NaN\ndtype: float64"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])\n",
    "obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.739407Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "0      blue\n2    purple\n4    yellow\ndtype: object"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj3 = Series([\"blue\", \"purple\", \"yellow\"], index=[0, 2, 4])\n",
    "obj3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.749769Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "data": {
      "text/plain": "0      blue\n1      blue\n2    purple\n3    purple\n4    yellow\n5    yellow\ndtype: object"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj3.reindex(np.arange(6), method=\"ffill\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.757767Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Texas  California\na     0      1           2\nc     3      4           5\nd     6      7           8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Texas</th>\n      <th>California</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.arange(9).reshape((3, 3)),\n",
    "                  index=[\"a\", \"c\", \"d\"],\n",
    "                  columns=[\"Ohio\", \"Texas\", \"California\"])\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.770230Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Texas  California\na   0.0    1.0         2.0\nb   NaN    NaN         NaN\nc   3.0    4.0         5.0\nd   6.0    7.0         8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Texas</th>\n      <th>California</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2 = frame.reindex(index=[\"a\", \"b\", \"c\", \"d\"])\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.777464Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "data": {
      "text/plain": "   Texas  Utah  California\na      1   NaN           2\nc      4   NaN           5\nd      7   NaN           8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Texas</th>\n      <th>Utah</th>\n      <th>California</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>NaN</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>7</td>\n      <td>NaN</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states = [\"Texas\", \"Utah\", \"California\"]\n",
    "frame.reindex(columns=states)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.788889Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 丢弃指定轴上的项"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\nc    2.0\nd    3.0\ne    4.0\ndtype: float64"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(np.arange(5.), index=[\"a\", \"b\", \"c\", \"d\", \"e\"])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.800380Z",
     "end_time": "2024-04-17T17:37:19.869126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\nd    3.0\ne    4.0\ndtype: float64"
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_obj = obj.drop('c')\n",
    "new_obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.806785Z",
     "end_time": "2024-04-17T17:37:19.870126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\ne    4.0\ndtype: float64"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.drop(['d', 'c'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.816176Z",
     "end_time": "2024-04-17T17:37:19.872130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nOhio        0    1      2     3\nColorado    4    5      6     7\nUtah        8    9     10    11\nNew York   12   13     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame(np.arange(16).reshape((4, 4)),\n",
    "                 index=[\"Ohio\", \"Colorado\", \"Utah\", \"New York\"],\n",
    "                 columns=[\"one\", \"two\", \"three\", \"four\"])\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.825460Z",
     "end_time": "2024-04-17T17:37:19.872130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nUtah        8    9     10    11\nNew York   12   13     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.drop(['Colorado', 'Ohio'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.833710Z",
     "end_time": "2024-04-17T17:37:19.872130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  three  four\nOhio        0      2     3\nColorado    4      6     7\nUtah        8     10    11\nNew York   12     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.drop('two', axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.841530Z",
     "end_time": "2024-04-17T17:37:19.872130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  three\nOhio        0      2\nColorado    4      6\nUtah        8     10\nNew York   12     14",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>three</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>14</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.drop(['two', 'four'], axis='columns')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.853405Z",
     "end_time": "2024-04-17T17:37:19.872130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\nd    3.0\ne    4.0\ndtype: float64"
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.drop('c', inplace=True)\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.862387Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 索引、选取和过滤"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\nc    2.0\nd    3.0\ndtype: float64"
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(np.arange(4.), index=[\"a\", \"b\", \"c\", \"d\"])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.869847Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj['b']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.879545Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.iloc[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.889341Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [
    {
     "data": {
      "text/plain": "c    2.0\nd    3.0\ndtype: float64"
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj[2:4]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.895918Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "data": {
      "text/plain": "b    1.0\na    0.0\nd    3.0\ndtype: float64"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj[['b', 'a', 'd']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.901888Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "outputs": [
    {
     "data": {
      "text/plain": "b    1.0\nd    3.0\ndtype: float64"
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.iloc[[1, 3]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.910688Z",
     "end_time": "2024-04-17T17:37:19.873130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\ndtype: float64"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj[obj < 2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.918295Z",
     "end_time": "2024-04-17T17:37:19.874130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "outputs": [
    {
     "data": {
      "text/plain": "b    1.0\nc    2.0\ndtype: float64"
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj['b':'c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.924769Z",
     "end_time": "2024-04-17T17:37:19.874130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    5.0\nc    5.0\nd    3.0\ndtype: float64"
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj['b':'c'] = 5\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.932957Z",
     "end_time": "2024-04-17T17:37:19.875130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nOhio        0    1      2     3\nColorado    4    5      6     7\nUtah        8    9     10    11\nNew York   12   13     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame(np.arange(16).reshape((4, 4)),\n",
    "                 index=[\"Ohio\", \"Colorado\", \"Utah\", \"New York\"],\n",
    "                 columns=[\"one\", \"two\", \"three\", \"four\"])\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.940343Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "data": {
      "text/plain": "Ohio         1\nColorado     5\nUtah         9\nNew York    13\nName: two, dtype: int32"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['two']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.947825Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [
    {
     "data": {
      "text/plain": "          three  one\nOhio          2    0\nColorado      6    4\nUtah         10    8\nNew York     14   12",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>three</th>\n      <th>one</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>6</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>10</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>14</td>\n      <td>12</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[['three', 'one']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.954805Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nOhio        0    1      2     3\nColorado    4    5      6     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.963009Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nColorado    4    5      6     7\nUtah        8    9     10    11\nNew York   12   13     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Colorado</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data['three'] > 5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.968104Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "data": {
      "text/plain": "            one    two  three   four\nOhio       True   True   True   True\nColorado   True  False  False  False\nUtah      False  False  False  False\nNew York  False  False  False  False",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data < 5"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.977920Z",
     "end_time": "2024-04-17T17:37:19.876136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three  four\nOhio        0    0      0     0\nColorado    0    5      6     7\nUtah        8    9     10    11\nNew York   12   13     14    15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>0</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data < 5] = 0\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.983584Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 用loc和iloc进行选取"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "outputs": [
    {
     "data": {
      "text/plain": "two      5\nthree    6\nName: Colorado, dtype: int32"
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc['Colorado', ['two', 'three']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:16.992408Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [
    {
     "data": {
      "text/plain": "four    11\none      8\ntwo      9\nName: Utah, dtype: int32"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[2, [3, 0, 1]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.002312Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "data": {
      "text/plain": "one       8\ntwo       9\nthree    10\nfour     11\nName: Utah, dtype: int32"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.007948Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "data": {
      "text/plain": "          four  one  two\nColorado     7    0    5\nUtah        11    8    9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>four</th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Colorado</th>\n      <td>7</td>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>11</td>\n      <td>8</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[[1, 2], [3, 0, 1]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.014032Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "outputs": [
    {
     "data": {
      "text/plain": "Ohio        0\nColorado    5\nUtah        9\nName: two, dtype: int32"
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[:'Utah', 'two']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.022489Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "outputs": [
    {
     "data": {
      "text/plain": "          one  two  three\nColorado    0    5      6\nUtah        8    9     10\nNew York   12   13     14",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Colorado</th>\n      <td>0</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>New York</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[:, :3][data.three > 5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.028394Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 整数索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "outputs": [
    {
     "data": {
      "text/plain": "0    0.0\n1    1.0\n2    2.0\ndtype: float64"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = Series(np.arange(3.))\n",
    "ser"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.035301Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.0\nb    1.0\nc    2.0\ndtype: float64"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser2 = Series(np.arange(3.), index=[\"a\", \"b\", \"c\"])\n",
    "ser2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.042275Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [
    {
     "data": {
      "text/plain": "2.0"
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.iloc[-1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.050259Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "outputs": [
    {
     "data": {
      "text/plain": "0    0.0\n1    1.0\ndtype: float64"
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser[:2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.054500Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 算术运算和数据对齐"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "outputs": [
    {
     "data": {
      "text/plain": "a    7.3\nc   -2.5\nd    3.4\ne    1.5\ndtype: float64"
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = Series([7.3, -2.5, 3.4, 1.5], index=[\"a\", \"c\", \"d\", \"e\"])\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.061527Z",
     "end_time": "2024-04-17T17:37:19.877136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "data": {
      "text/plain": "a   -2.1\nc    3.6\ne   -1.5\nf    4.0\ng    3.1\ndtype: float64"
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = Series([-2.1, 3.6, -1.5, 4, 3.1], index=[\"a\", \"c\", \"e\", \"f\", \"g\"])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.067493Z",
     "end_time": "2024-04-17T17:37:19.878128Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "outputs": [
    {
     "data": {
      "text/plain": "a    5.2\nc    1.1\nd    NaN\ne    0.0\nf    NaN\ng    NaN\ndtype: float64"
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.074899Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "outputs": [
    {
     "data": {
      "text/plain": "            b    c    d\nOhio      0.0  1.0  2.0\nTexas     3.0  4.0  5.0\nColorado  6.0  7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame(np.arange(9.).reshape((3, 3)), columns=list(\"bcd\"), index=[\"Ohio\", \"Texas\", \"Colorado\"])\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.081033Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "outputs": [
    {
     "data": {
      "text/plain": "          b     d     e\nUtah    0.0   1.0   2.0\nOhio    3.0   4.0   5.0\nTexas   6.0   7.0   8.0\nOregon  9.0  10.0  11.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>11.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = DataFrame(np.arange(12.).reshape((4, 3)), columns=list(\"bde\"), index=[\"Utah\", \"Ohio\", \"Texas\", \"Oregon\"])\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.092807Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "outputs": [
    {
     "data": {
      "text/plain": "            b   c     d   e\nColorado  NaN NaN   NaN NaN\nOhio      3.0 NaN   6.0 NaN\nOregon    NaN NaN   NaN NaN\nTexas     9.0 NaN  12.0 NaN\nUtah      NaN NaN   NaN NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Colorado</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>3.0</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>9.0</td>\n      <td>NaN</td>\n      <td>12.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Utah</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.101227Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "outputs": [
    {
     "data": {
      "text/plain": "   A\n0  1\n1  2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame({\"A\": [1, 2]})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.111898Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "outputs": [
    {
     "data": {
      "text/plain": "   B\n0  3\n1  4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = DataFrame({\"B\": [3, 4]})\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.118458Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "outputs": [
    {
     "data": {
      "text/plain": "    A   B\n0 NaN NaN\n1 NaN NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.125070Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 在算术方法中填充值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [
    {
     "data": {
      "text/plain": "     a    b     c     d\n0  0.0  1.0   2.0   3.0\n1  4.0  5.0   6.0   7.0\n2  8.0  9.0  10.0  11.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.0</td>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>11.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns=list(\"abcd\"))\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.132871Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "outputs": [
    {
     "data": {
      "text/plain": "      a     b     c     d     e\n0   0.0   1.0   2.0   3.0   4.0\n1   5.0   6.0   7.0   8.0   9.0\n2  10.0  11.0  12.0  13.0  14.0\n3  15.0  16.0  17.0  18.0  19.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>10.0</td>\n      <td>11.0</td>\n      <td>12.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>15.0</td>\n      <td>16.0</td>\n      <td>17.0</td>\n      <td>18.0</td>\n      <td>19.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns=list(\"abcde\"))\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.145241Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "outputs": [
    {
     "data": {
      "text/plain": "      a     b     c     d     e\n0   0.0   1.0   2.0   3.0   4.0\n1   5.0   NaN   7.0   8.0   9.0\n2  10.0  11.0  12.0  13.0  14.0\n3  15.0  16.0  17.0  18.0  19.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>5.0</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>10.0</td>\n      <td>11.0</td>\n      <td>12.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>15.0</td>\n      <td>16.0</td>\n      <td>17.0</td>\n      <td>18.0</td>\n      <td>19.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc[1, \"b\"] = np.nan\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.154712Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "outputs": [
    {
     "data": {
      "text/plain": "      a     b     c     d   e\n0   0.0   2.0   4.0   6.0 NaN\n1   9.0   NaN  13.0  15.0 NaN\n2  18.0  20.0  22.0  24.0 NaN\n3   NaN   NaN   NaN   NaN NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>4.0</td>\n      <td>6.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>9.0</td>\n      <td>NaN</td>\n      <td>13.0</td>\n      <td>15.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>18.0</td>\n      <td>20.0</td>\n      <td>22.0</td>\n      <td>24.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.163607Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "outputs": [
    {
     "data": {
      "text/plain": "      a     b     c     d     e\n0   0.0   2.0   4.0   6.0   4.0\n1   9.0   5.0  13.0  15.0   9.0\n2  18.0  20.0  22.0  24.0  14.0\n3  15.0  16.0  17.0  18.0  19.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>4.0</td>\n      <td>6.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>9.0</td>\n      <td>5.0</td>\n      <td>13.0</td>\n      <td>15.0</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>18.0</td>\n      <td>20.0</td>\n      <td>22.0</td>\n      <td>24.0</td>\n      <td>14.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>15.0</td>\n      <td>16.0</td>\n      <td>17.0</td>\n      <td>18.0</td>\n      <td>19.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.add(df2, fill_value=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.235212Z",
     "end_time": "2024-04-17T17:37:19.879129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "outputs": [
    {
     "data": {
      "text/plain": "       a         b         c         d\n0    inf  1.000000  0.500000  0.333333\n1  0.250  0.200000  0.166667  0.142857\n2  0.125  0.111111  0.100000  0.090909",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>inf</td>\n      <td>1.000000</td>\n      <td>0.500000</td>\n      <td>0.333333</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.250</td>\n      <td>0.200000</td>\n      <td>0.166667</td>\n      <td>0.142857</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.125</td>\n      <td>0.111111</td>\n      <td>0.100000</td>\n      <td>0.090909</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.238212Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "outputs": [
    {
     "data": {
      "text/plain": "       a         b         c         d\n0    inf  1.000000  0.500000  0.333333\n1  0.250  0.200000  0.166667  0.142857\n2  0.125  0.111111  0.100000  0.090909",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>inf</td>\n      <td>1.000000</td>\n      <td>0.500000</td>\n      <td>0.333333</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.250</td>\n      <td>0.200000</td>\n      <td>0.166667</td>\n      <td>0.142857</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.125</td>\n      <td>0.111111</td>\n      <td>0.100000</td>\n      <td>0.090909</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.rdiv(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.244193Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "outputs": [
    {
     "data": {
      "text/plain": "     a    b     c     d  e\n0  0.0  1.0   2.0   3.0  0\n1  4.0  5.0   6.0   7.0  0\n2  8.0  9.0  10.0  11.0  0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.0</td>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>11.0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.reindex(columns=df2.columns, fill_value=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.256490Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### DataFrame和Series之间的运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0.,  1.,  2.,  3.],\n       [ 4.,  5.,  6.,  7.],\n       [ 8.,  9., 10., 11.]])"
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(12.).reshape((3, 4))\n",
    "arr"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.264140Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0., 1., 2., 3.])"
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.271042Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0., 0., 0., 0.],\n       [4., 4., 4., 4.],\n       [8., 8., 8., 8.]])"
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr - arr[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.279190Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "outputs": [
    {
     "data": {
      "text/plain": "          b     d     e\nUtah    0.0   1.0   2.0\nOhio    3.0   4.0   5.0\nTexas   6.0   7.0   8.0\nOregon  9.0  10.0  11.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>11.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.arange(12.).reshape((4, 3)),\n",
    "                  columns=list(\"bde\"),\n",
    "                  index=[\"Utah\", \"Ohio\", \"Texas\", \"Oregon\"])\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.283309Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "outputs": [
    {
     "data": {
      "text/plain": "b    0.0\nd    1.0\ne    2.0\nName: Utah, dtype: float64"
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series = frame.iloc[0]\n",
    "series"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.293292Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "outputs": [
    {
     "data": {
      "text/plain": "          b    d    e\nUtah    0.0  0.0  0.0\nOhio    3.0  3.0  3.0\nTexas   6.0  6.0  6.0\nOregon  9.0  9.0  9.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>3.0</td>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>6.0</td>\n      <td>6.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>9.0</td>\n      <td>9.0</td>\n      <td>9.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame - series"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.300727Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "outputs": [
    {
     "data": {
      "text/plain": "b    0\ne    1\nf    2\ndtype: int32"
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series2 = Series(np.arange(3), index=[\"b\", \"e\", \"f\"])\n",
    "series2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.320025Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "outputs": [
    {
     "data": {
      "text/plain": "          b   d     e   f\nUtah    0.0 NaN   3.0 NaN\nOhio    3.0 NaN   6.0 NaN\nTexas   6.0 NaN   9.0 NaN\nOregon  9.0 NaN  12.0 NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>3.0</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>6.0</td>\n      <td>NaN</td>\n      <td>9.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>9.0</td>\n      <td>NaN</td>\n      <td>12.0</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame + series2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.327035Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "outputs": [
    {
     "data": {
      "text/plain": "Utah       1.0\nOhio       4.0\nTexas      7.0\nOregon    10.0\nName: d, dtype: float64"
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series3 = frame['d']\n",
    "series3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.336123Z",
     "end_time": "2024-04-17T17:37:19.880129Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "outputs": [
    {
     "data": {
      "text/plain": "          b    d    e\nUtah   -1.0  0.0  1.0\nOhio   -1.0  0.0  1.0\nTexas  -1.0  0.0  1.0\nOregon -1.0  0.0  1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>-1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>-1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>-1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>-1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sub(series3, axis='index')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.344149Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 函数应用和映射"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "outputs": [
    {
     "data": {
      "text/plain": "               b         d         e\nUtah   -0.204708  0.478943 -0.519439\nOhio   -0.555730  1.965781  1.393406\nTexas   0.092908  0.281746  0.769023\nOregon  1.246435  1.007189 -1.296221",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>-0.204708</td>\n      <td>0.478943</td>\n      <td>-0.519439</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>-0.555730</td>\n      <td>1.965781</td>\n      <td>1.393406</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>0.092908</td>\n      <td>0.281746</td>\n      <td>0.769023</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>1.246435</td>\n      <td>1.007189</td>\n      <td>-1.296221</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.random.standard_normal((4, 3)),\n",
    "                  columns=list(\"bde\"),\n",
    "                  index=[\"Utah\", \"Ohio\", \"Texas\", \"Oregon\"])\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.353403Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "outputs": [
    {
     "data": {
      "text/plain": "               b         d         e\nUtah    0.204708  0.478943  0.519439\nOhio    0.555730  1.965781  1.393406\nTexas   0.092908  0.281746  0.769023\nOregon  1.246435  1.007189  1.296221",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>0.204708</td>\n      <td>0.478943</td>\n      <td>0.519439</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>0.555730</td>\n      <td>1.965781</td>\n      <td>1.393406</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>0.092908</td>\n      <td>0.281746</td>\n      <td>0.769023</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>1.246435</td>\n      <td>1.007189</td>\n      <td>1.296221</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(frame)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.360145Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "outputs": [
    {
     "data": {
      "text/plain": "b    1.802165\nd    1.684034\ne    2.689627\ndtype: float64"
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = lambda x: x.max() - x.min()\n",
    "frame.apply(f)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.400144Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "outputs": [
    {
     "data": {
      "text/plain": "Utah      0.998382\nOhio      2.521511\nTexas     0.676115\nOregon    2.542656\ndtype: float64"
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.apply(f, axis=\"columns\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.405818Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "outputs": [
    {
     "data": {
      "text/plain": "            b         d         e\nmin -0.555730  0.281746 -1.296221\nmax  1.246435  1.965781  1.393406",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>min</th>\n      <td>-0.555730</td>\n      <td>0.281746</td>\n      <td>-1.296221</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>1.246435</td>\n      <td>1.965781</td>\n      <td>1.393406</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = lambda x: Series([x.min(), x.max()], index=[\"min\", \"max\"])\n",
    "frame.apply(f)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.410668Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "outputs": [
    {
     "data": {
      "text/plain": "            b     d      e\nUtah    -0.20  0.48  -0.52\nOhio    -0.56  1.97   1.39\nTexas    0.09  0.28   0.77\nOregon   1.25  1.01  -1.30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Utah</th>\n      <td>-0.20</td>\n      <td>0.48</td>\n      <td>-0.52</td>\n    </tr>\n    <tr>\n      <th>Ohio</th>\n      <td>-0.56</td>\n      <td>1.97</td>\n      <td>1.39</td>\n    </tr>\n    <tr>\n      <th>Texas</th>\n      <td>0.09</td>\n      <td>0.28</td>\n      <td>0.77</td>\n    </tr>\n    <tr>\n      <th>Oregon</th>\n      <td>1.25</td>\n      <td>1.01</td>\n      <td>-1.30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_format = lambda x: '%.2f' % x\n",
    "frame.map(_format)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.421587Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "outputs": [
    {
     "data": {
      "text/plain": "Utah      -0.52\nOhio       1.39\nTexas      0.77\nOregon    -1.30\nName: e, dtype: object"
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame[\"e\"].map(_format)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.429256Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 排序和排名"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\nd    0\ndtype: int32"
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(np.arange(4), index=[\"d\", \"a\", \"b\", \"c\"])\n",
    "obj.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.435692Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "outputs": [
    {
     "data": {
      "text/plain": "       d  a  b  c\nthree  0  1  2  3\none    4  5  6  7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>d</th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>three</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>one</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.arange(8).reshape((2, 4)),\n",
    "                  index=[\"three\", \"one\"],\n",
    "                  columns=[\"d\", \"a\", \"b\", \"c\"])\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.443788Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "outputs": [
    {
     "data": {
      "text/plain": "       d  a  b  c\none    4  5  6  7\nthree  0  1  2  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>d</th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.453171Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "outputs": [
    {
     "data": {
      "text/plain": "       a  b  c  d\nthree  1  2  3  0\none    5  6  7  4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>three</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>one</th>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_index(axis=\"columns\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.461569Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "outputs": [
    {
     "data": {
      "text/plain": "       d  c  b  a\nthree  0  3  2  1\none    4  7  6  5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>d</th>\n      <th>c</th>\n      <th>b</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>three</th>\n      <td>0</td>\n      <td>3</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>one</th>\n      <td>4</td>\n      <td>7</td>\n      <td>6</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_index(axis=\"columns\", ascending=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.469201Z",
     "end_time": "2024-04-17T17:37:19.881130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "outputs": [
    {
     "data": {
      "text/plain": "2   -3\n3    2\n0    4\n1    7\ndtype: int64"
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([4, 7, -3, 2])\n",
    "obj.sort_values()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.475982Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "outputs": [
    {
     "data": {
      "text/plain": "4   -3.0\n5    2.0\n0    4.0\n2    7.0\n1    NaN\n3    NaN\ndtype: float64"
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([4, np.nan, 7, np.nan, -3, 2])\n",
    "obj.sort_values()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.483576Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "outputs": [
    {
     "data": {
      "text/plain": "1    NaN\n3    NaN\n4   -3.0\n5    2.0\n0    4.0\n2    7.0\ndtype: float64"
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.sort_values(na_position=\"first\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.489260Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "outputs": [
    {
     "data": {
      "text/plain": "   b  a\n0  4  0\n1  7  1\n2 -3  0\n3  2  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame({\"b\": [4, 7, -3, 2], \"a\": [0, 1, 0, 1]})\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.497320Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "outputs": [
    {
     "data": {
      "text/plain": "   b  a\n2 -3  0\n3  2  1\n0  4  0\n1  7  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>-3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_values(\"b\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.505079Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "outputs": [
    {
     "data": {
      "text/plain": "   b  a\n2 -3  0\n0  4  0\n3  2  1\n1  7  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>-3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_values([\"a\", \"b\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.510250Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "outputs": [
    {
     "data": {
      "text/plain": "0    6.5\n1    1.0\n2    6.5\n3    4.5\n4    3.0\n5    2.0\n6    4.5\ndtype: float64"
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([7, -5, 7, 4, 2, 0, 4])\n",
    "obj.rank()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.518039Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "outputs": [
    {
     "data": {
      "text/plain": "0    6.0\n1    1.0\n2    7.0\n3    4.0\n4    3.0\n5    2.0\n6    5.0\ndtype: float64"
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.rank(method=\"first\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.525840Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1.5\n1    7.0\n2    1.5\n3    3.5\n4    5.0\n5    6.0\n6    3.5\ndtype: float64"
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.rank(ascending=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.532986Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "outputs": [
    {
     "data": {
      "text/plain": "     b  a    c\n0  4.3  0 -2.0\n1  7.0  1  5.0\n2 -3.0  0  8.0\n3  2.0  1 -2.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>a</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4.3</td>\n      <td>0</td>\n      <td>-2.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7.0</td>\n      <td>1</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-3.0</td>\n      <td>0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2.0</td>\n      <td>1</td>\n      <td>-2.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame({\"b\": [4.3, 7, -3, 2], \"a\": [0, 1, 0, 1], \"c\": [-2, 5, 8, -2.5]})\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.538265Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "outputs": [
    {
     "data": {
      "text/plain": "     b    a    c\n0  3.0  2.0  1.0\n1  3.0  1.0  2.0\n2  1.0  2.0  3.0\n3  3.0  2.0  1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>a</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>3.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.rank(axis=\"columns\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.546804Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 带有重复标签的轴索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0\na    1\nb    2\nb    3\nc    4\ndtype: int32"
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(np.arange(5), index=[\"a\", \"a\", \"b\", \"b\", \"c\"])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.556042Z",
     "end_time": "2024-04-17T17:37:19.882130Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.index.is_unique"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.561708Z",
     "end_time": "2024-04-17T17:37:19.883131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0\na    1\ndtype: int32"
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj['a']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.568325Z",
     "end_time": "2024-04-17T17:37:19.883131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "outputs": [
    {
     "data": {
      "text/plain": "4"
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj['c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.574514Z",
     "end_time": "2024-04-17T17:37:19.883131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\na  0.274992  0.228913  1.352917\na  0.886429 -2.001637 -0.371843\nb  1.669025 -0.438570 -0.539741\nb  0.476985  3.248944 -1.021228",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.274992</td>\n      <td>0.228913</td>\n      <td>1.352917</td>\n    </tr>\n    <tr>\n      <th>a</th>\n      <td>0.886429</td>\n      <td>-2.001637</td>\n      <td>-0.371843</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>1.669025</td>\n      <td>-0.438570</td>\n      <td>-0.539741</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.476985</td>\n      <td>3.248944</td>\n      <td>-1.021228</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame(np.random.standard_normal((4, 3)), index=[\"a\", \"a\", \"b\", \"b\"])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.579790Z",
     "end_time": "2024-04-17T17:37:19.883131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\nb  1.669025 -0.438570 -0.539741\nb  0.476985  3.248944 -1.021228",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>b</th>\n      <td>1.669025</td>\n      <td>-0.438570</td>\n      <td>-0.539741</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.476985</td>\n      <td>3.248944</td>\n      <td>-1.021228</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['b']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.588840Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.3 汇总和计算描述统计"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "outputs": [
    {
     "data": {
      "text/plain": "    one  two\na  1.40  NaN\nb  7.10 -4.5\nc   NaN  NaN\nd  0.75 -1.3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.40</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>7.10</td>\n      <td>-4.5</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>0.75</td>\n      <td>-1.3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame([[1.4, np.nan], [7.1, -4.5],\n",
    "                [np.nan, np.nan], [0.75, -1.3]],\n",
    "               index=[\"a\", \"b\", \"c\", \"d\"],\n",
    "               columns=[\"one\", \"two\"])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.595334Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "outputs": [
    {
     "data": {
      "text/plain": "one    9.25\ntwo   -5.80\ndtype: float64"
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.604240Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "outputs": [
    {
     "data": {
      "text/plain": "a    1.40\nb    2.60\nc    0.00\nd   -0.55\ndtype: float64"
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.610155Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "outputs": [
    {
     "data": {
      "text/plain": "a      NaN\nb    1.300\nc      NaN\nd   -0.275\ndtype: float64"
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.mean(axis='columns', skipna=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.617943Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "outputs": [
    {
     "data": {
      "text/plain": "one    b\ntwo    d\ndtype: object"
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.idxmax()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.624008Z",
     "end_time": "2024-04-17T17:37:19.884131Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "outputs": [
    {
     "data": {
      "text/plain": "    one  two\na  1.40  NaN\nb  8.50 -4.5\nc   NaN  NaN\nd  9.25 -5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.40</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>8.50</td>\n      <td>-4.5</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>9.25</td>\n      <td>-5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumsum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.637100Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "outputs": [
    {
     "data": {
      "text/plain": "            one       two\ncount  3.000000  2.000000\nmean   3.083333 -2.900000\nstd    3.493685  2.262742\nmin    0.750000 -4.500000\n25%    1.075000 -3.700000\n50%    1.400000 -2.900000\n75%    4.250000 -2.100000\nmax    7.100000 -1.300000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>3.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>3.083333</td>\n      <td>-2.900000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>3.493685</td>\n      <td>2.262742</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>0.750000</td>\n      <td>-4.500000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1.075000</td>\n      <td>-3.700000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>1.400000</td>\n      <td>-2.900000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>4.250000</td>\n      <td>-2.100000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>7.100000</td>\n      <td>-1.300000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.641104Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "outputs": [
    {
     "data": {
      "text/plain": "0     a\n1     a\n2     b\n3     c\n4     a\n5     a\n6     b\n7     c\n8     a\n9     a\n10    b\n11    c\n12    a\n13    a\n14    b\n15    c\ndtype: object"
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series(['a', 'a', 'b', 'c'] * 4)\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.651128Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "outputs": [
    {
     "data": {
      "text/plain": "count     16\nunique     3\ntop        a\nfreq       8\ndtype: object"
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.658096Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 相关系数和协方差"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "outputs": [
    {
     "data": {
      "text/plain": "                  AAPL        GOOG         IBM       MSFT\nDate                                                     \n2010-01-04   27.990226  313.062468  113.304536  25.884104\n2010-01-05   28.038618  311.683844  111.935822  25.892466\n2010-01-06   27.592626  303.826685  111.208683  25.733566\n2010-01-07   27.541619  296.753749  110.823732  25.465944\n2010-01-08   27.724725  300.709808  111.935822  25.641571\n...                ...         ...         ...        ...\n2016-10-17  117.550003  779.960022  154.770004  57.220001\n2016-10-18  117.470001  795.260010  150.720001  57.660000\n2016-10-19  117.120003  801.500000  151.259995  57.529999\n2016-10-20  117.059998  796.969971  151.520004  57.250000\n2016-10-21  116.599998  799.369995  149.630005  59.660000\n\n[1714 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>AAPL</th>\n      <th>GOOG</th>\n      <th>IBM</th>\n      <th>MSFT</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2010-01-04</th>\n      <td>27.990226</td>\n      <td>313.062468</td>\n      <td>113.304536</td>\n      <td>25.884104</td>\n    </tr>\n    <tr>\n      <th>2010-01-05</th>\n      <td>28.038618</td>\n      <td>311.683844</td>\n      <td>111.935822</td>\n      <td>25.892466</td>\n    </tr>\n    <tr>\n      <th>2010-01-06</th>\n      <td>27.592626</td>\n      <td>303.826685</td>\n      <td>111.208683</td>\n      <td>25.733566</td>\n    </tr>\n    <tr>\n      <th>2010-01-07</th>\n      <td>27.541619</td>\n      <td>296.753749</td>\n      <td>110.823732</td>\n      <td>25.465944</td>\n    </tr>\n    <tr>\n      <th>2010-01-08</th>\n      <td>27.724725</td>\n      <td>300.709808</td>\n      <td>111.935822</td>\n      <td>25.641571</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2016-10-17</th>\n      <td>117.550003</td>\n      <td>779.960022</td>\n      <td>154.770004</td>\n      <td>57.220001</td>\n    </tr>\n    <tr>\n      <th>2016-10-18</th>\n      <td>117.470001</td>\n      <td>795.260010</td>\n      <td>150.720001</td>\n      <td>57.660000</td>\n    </tr>\n    <tr>\n      <th>2016-10-19</th>\n      <td>117.120003</td>\n      <td>801.500000</td>\n      <td>151.259995</td>\n      <td>57.529999</td>\n    </tr>\n    <tr>\n      <th>2016-10-20</th>\n      <td>117.059998</td>\n      <td>796.969971</td>\n      <td>151.520004</td>\n      <td>57.250000</td>\n    </tr>\n    <tr>\n      <th>2016-10-21</th>\n      <td>116.599998</td>\n      <td>799.369995</td>\n      <td>149.630005</td>\n      <td>59.660000</td>\n    </tr>\n  </tbody>\n</table>\n<p>1714 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price = pd.read_pickle(\"examples/yahoo_price.pkl\")\n",
    "price"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.664613Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "outputs": [
    {
     "data": {
      "text/plain": "                 AAPL      GOOG       IBM      MSFT\nDate                                               \n2010-01-04  123432400   3927000   6155300  38409100\n2010-01-05  150476200   6031900   6841400  49749600\n2010-01-06  138040000   7987100   5605300  58182400\n2010-01-07  119282800  12876600   5840600  50559700\n2010-01-08  111902700   9483900   4197200  51197400\n...               ...       ...       ...       ...\n2016-10-17   23624900   1089500   5890400  23830000\n2016-10-18   24553500   1995600  12770600  19149500\n2016-10-19   20034600    116600   4632900  22878400\n2016-10-20   24125800   1734200   4023100  49455600\n2016-10-21   22384800   1260500   4401900  79974200\n\n[1714 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>AAPL</th>\n      <th>GOOG</th>\n      <th>IBM</th>\n      <th>MSFT</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2010-01-04</th>\n      <td>123432400</td>\n      <td>3927000</td>\n      <td>6155300</td>\n      <td>38409100</td>\n    </tr>\n    <tr>\n      <th>2010-01-05</th>\n      <td>150476200</td>\n      <td>6031900</td>\n      <td>6841400</td>\n      <td>49749600</td>\n    </tr>\n    <tr>\n      <th>2010-01-06</th>\n      <td>138040000</td>\n      <td>7987100</td>\n      <td>5605300</td>\n      <td>58182400</td>\n    </tr>\n    <tr>\n      <th>2010-01-07</th>\n      <td>119282800</td>\n      <td>12876600</td>\n      <td>5840600</td>\n      <td>50559700</td>\n    </tr>\n    <tr>\n      <th>2010-01-08</th>\n      <td>111902700</td>\n      <td>9483900</td>\n      <td>4197200</td>\n      <td>51197400</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2016-10-17</th>\n      <td>23624900</td>\n      <td>1089500</td>\n      <td>5890400</td>\n      <td>23830000</td>\n    </tr>\n    <tr>\n      <th>2016-10-18</th>\n      <td>24553500</td>\n      <td>1995600</td>\n      <td>12770600</td>\n      <td>19149500</td>\n    </tr>\n    <tr>\n      <th>2016-10-19</th>\n      <td>20034600</td>\n      <td>116600</td>\n      <td>4632900</td>\n      <td>22878400</td>\n    </tr>\n    <tr>\n      <th>2016-10-20</th>\n      <td>24125800</td>\n      <td>1734200</td>\n      <td>4023100</td>\n      <td>49455600</td>\n    </tr>\n    <tr>\n      <th>2016-10-21</th>\n      <td>22384800</td>\n      <td>1260500</td>\n      <td>4401900</td>\n      <td>79974200</td>\n    </tr>\n  </tbody>\n</table>\n<p>1714 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "volume = pd.read_pickle(\"examples/yahoo_volume.pkl\")\n",
    "volume"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.675713Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "outputs": [
    {
     "data": {
      "text/plain": "                AAPL      GOOG       IBM      MSFT\nDate                                              \n2016-10-17 -0.000680  0.001837  0.002072 -0.003483\n2016-10-18 -0.000681  0.019616 -0.026168  0.007690\n2016-10-19 -0.002979  0.007846  0.003583 -0.002255\n2016-10-20 -0.000512 -0.005652  0.001719 -0.004867\n2016-10-21 -0.003930  0.003011 -0.012474  0.042096",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>AAPL</th>\n      <th>GOOG</th>\n      <th>IBM</th>\n      <th>MSFT</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2016-10-17</th>\n      <td>-0.000680</td>\n      <td>0.001837</td>\n      <td>0.002072</td>\n      <td>-0.003483</td>\n    </tr>\n    <tr>\n      <th>2016-10-18</th>\n      <td>-0.000681</td>\n      <td>0.019616</td>\n      <td>-0.026168</td>\n      <td>0.007690</td>\n    </tr>\n    <tr>\n      <th>2016-10-19</th>\n      <td>-0.002979</td>\n      <td>0.007846</td>\n      <td>0.003583</td>\n      <td>-0.002255</td>\n    </tr>\n    <tr>\n      <th>2016-10-20</th>\n      <td>-0.000512</td>\n      <td>-0.005652</td>\n      <td>0.001719</td>\n      <td>-0.004867</td>\n    </tr>\n    <tr>\n      <th>2016-10-21</th>\n      <td>-0.003930</td>\n      <td>0.003011</td>\n      <td>-0.012474</td>\n      <td>0.042096</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns = price.pct_change()\n",
    "returns.tail()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.685758Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "outputs": [
    {
     "data": {
      "text/plain": "0.49976361144151155"
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns[\"MSFT\"].corr(returns[\"IBM\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.696879Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "outputs": [
    {
     "data": {
      "text/plain": "8.870655479703546e-05"
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns[\"MSFT\"].cov(returns[\"IBM\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.702814Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "outputs": [
    {
     "data": {
      "text/plain": "          AAPL      GOOG       IBM      MSFT\nAAPL  1.000000  0.407919  0.386817  0.389695\nGOOG  0.407919  1.000000  0.405099  0.465919\nIBM   0.386817  0.405099  1.000000  0.499764\nMSFT  0.389695  0.465919  0.499764  1.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>AAPL</th>\n      <th>GOOG</th>\n      <th>IBM</th>\n      <th>MSFT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>AAPL</th>\n      <td>1.000000</td>\n      <td>0.407919</td>\n      <td>0.386817</td>\n      <td>0.389695</td>\n    </tr>\n    <tr>\n      <th>GOOG</th>\n      <td>0.407919</td>\n      <td>1.000000</td>\n      <td>0.405099</td>\n      <td>0.465919</td>\n    </tr>\n    <tr>\n      <th>IBM</th>\n      <td>0.386817</td>\n      <td>0.405099</td>\n      <td>1.000000</td>\n      <td>0.499764</td>\n    </tr>\n    <tr>\n      <th>MSFT</th>\n      <td>0.389695</td>\n      <td>0.465919</td>\n      <td>0.499764</td>\n      <td>1.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns.corr()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.708889Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "outputs": [
    {
     "data": {
      "text/plain": "          AAPL      GOOG       IBM      MSFT\nAAPL  0.000277  0.000107  0.000078  0.000095\nGOOG  0.000107  0.000251  0.000078  0.000108\nIBM   0.000078  0.000078  0.000146  0.000089\nMSFT  0.000095  0.000108  0.000089  0.000215",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>AAPL</th>\n      <th>GOOG</th>\n      <th>IBM</th>\n      <th>MSFT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>AAPL</th>\n      <td>0.000277</td>\n      <td>0.000107</td>\n      <td>0.000078</td>\n      <td>0.000095</td>\n    </tr>\n    <tr>\n      <th>GOOG</th>\n      <td>0.000107</td>\n      <td>0.000251</td>\n      <td>0.000078</td>\n      <td>0.000108</td>\n    </tr>\n    <tr>\n      <th>IBM</th>\n      <td>0.000078</td>\n      <td>0.000078</td>\n      <td>0.000146</td>\n      <td>0.000089</td>\n    </tr>\n    <tr>\n      <th>MSFT</th>\n      <td>0.000095</td>\n      <td>0.000108</td>\n      <td>0.000089</td>\n      <td>0.000215</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns.cov()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.716757Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "outputs": [
    {
     "data": {
      "text/plain": "AAPL    0.386817\nGOOG    0.405099\nIBM     1.000000\nMSFT    0.499764\ndtype: float64"
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns.corrwith(returns[\"IBM\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.725440Z",
     "end_time": "2024-04-17T17:37:19.885132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "outputs": [
    {
     "data": {
      "text/plain": "AAPL   -0.075565\nGOOG   -0.007067\nIBM    -0.204849\nMSFT   -0.092950\ndtype: float64"
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "returns.corrwith(volume)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.733385Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 唯一值、值计数以及成员资格"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "outputs": [
    {
     "data": {
      "text/plain": "0    c\n1    a\n2    d\n3    a\n4    a\n5    b\n6    b\n7    c\n8    c\ndtype: object"
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = Series([\"c\", \"a\", \"d\", \"a\", \"a\", \"b\", \"b\", \"c\", \"c\"])\n",
    "obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.745985Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['c', 'a', 'd', 'b'], dtype=object)"
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uniques = obj.unique()\n",
    "uniques"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.747989Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "outputs": [
    {
     "data": {
      "text/plain": "c    3\na    3\nb    2\nd    1\nName: count, dtype: int64"
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.753914Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\HP\\AppData\\Local\\Temp\\ipykernel_16508\\4234849955.py:1: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.\n",
      "  pd.value_counts(obj.values, sort=False)\n"
     ]
    },
    {
     "data": {
      "text/plain": "c    3\na    3\nd    1\nb    2\nName: count, dtype: int64"
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(obj.values, sort=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.761906Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "outputs": [
    {
     "data": {
      "text/plain": "0     True\n1    False\n2    False\n3    False\n4    False\n5     True\n6     True\n7     True\n8     True\ndtype: bool"
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = obj.isin(['b', 'c'])\n",
    "mask"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.770596Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "outputs": [
    {
     "data": {
      "text/plain": "0    c\n5    b\n6    b\n7    c\n8    c\ndtype: object"
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj[mask]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.777877Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 2, 1, 1, 0, 2], dtype=int64)"
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_match = Series([\"c\", \"a\", \"b\", \"b\", \"c\", \"a\"])\n",
    "unique_vals = Series([\"c\", \"b\", \"a\"])\n",
    "indices = pd.Index(unique_vals).get_indexer(to_match)\n",
    "indices"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.783786Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "outputs": [
    {
     "data": {
      "text/plain": "   Qu1  Qu2  Qu3\n0    1    2    1\n1    3    3    5\n2    4    1    2\n3    3    2    4\n4    4    3    4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Qu1</th>\n      <th>Qu2</th>\n      <th>Qu3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>3</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>2</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame({\"Qu1\": [1, 3, 4, 3, 4],\n",
    "                  \"Qu2\": [2, 3, 1, 2, 3],\n",
    "                  \"Qu3\": [1, 5, 2, 4, 4]})\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.789437Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "outputs": [
    {
     "data": {
      "text/plain": "Qu1\n1    1\n3    2\n4    2\nName: count, dtype: int64"
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"Qu1\"].value_counts().sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.796631Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\HP\\AppData\\Local\\Temp\\ipykernel_16508\\2387387694.py:1: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.\n",
      "  result = data.apply(pd.value_counts).fillna(0)\n"
     ]
    },
    {
     "data": {
      "text/plain": "   Qu1  Qu2  Qu3\n1  1.0  1.0  1.0\n2  0.0  2.0  1.0\n3  2.0  2.0  0.0\n4  2.0  0.0  2.0\n5  0.0  0.0  1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Qu1</th>\n      <th>Qu2</th>\n      <th>Qu3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = data.apply(pd.value_counts).fillna(0)\n",
    "result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.804635Z",
     "end_time": "2024-04-17T17:37:19.886132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "outputs": [
    {
     "data": {
      "text/plain": "a  b\n1  0    2\n2  0    2\n1  1    1\nName: count, dtype: int64"
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame({\"a\": [1, 1, 1, 2, 2], \"b\": [0, 0, 1, 0, 0]})\n",
    "data.value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.814813Z",
     "end_time": "2024-04-17T17:37:19.887132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-17T17:37:17.823089Z",
     "end_time": "2024-04-17T17:37:19.887132Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
